<template>
  <view class="product-card" @click="handleClick">
    <view class="product-image">
      <image 
        :src="product.image_url || '/static/icons/default-product.png'" 
        mode="aspectFill"
        class="image"
        :lazy-load="true"
      />
      <view class="tags">
        <view v-if="product.is_hot" class="tag hot-tag">热门</view>
        <view v-if="product.is_new" class="tag new-tag">新品</view>
      </view>
      <view v-if="product.stock_quantity <= 0" class="out-of-stock-overlay">
        <text class="out-of-stock-text">缺货</text>
      </view>
    </view>
    
    <view class="product-info">
      <view class="product-name">{{ product.name }}</view>
      <view class="product-desc" v-if="showDescription">
        {{ product.description || '暂无描述' }}
      </view>
      
      <view class="product-price">
        <view class="points-price">
          <text class="price-number">{{ product.points_price }}</text>
          <text class="price-unit">积分</text>
        </view>
        <view v-if="product.original_price" class="original-price">
          原价: ¥{{ product.original_price }}
        </view>
      </view>
      
      <view class="product-footer">
        <view class="product-stock">
          <text v-if="product.stock_quantity > 0" class="in-stock">
            库存: {{ product.stock_quantity }}
          </text>
          <text v-else class="out-of-stock">缺货</text>
        </view>
        
        <view v-if="showActions" class="product-actions">
          <button 
            class="action-btn"
            :disabled="product.stock_quantity <= 0"
            @click.stop="handleExchange"
          >
            {{ product.stock_quantity > 0 ? '兑换' : '缺货' }}
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'ProductCard',
  props: {
    product: {
      type: Object,
      required: true
    },
    showDescription: {
      type: Boolean,
      default: true
    },
    showActions: {
      type: Boolean,
      default: false
    },
    layout: {
      type: String,
      default: 'vertical', // vertical | horizontal
      validator: value => ['vertical', 'horizontal'].includes(value)
    }
  },
  
  methods: {
    handleClick() {
      this.$emit('click', this.product)
    },
    
    handleExchange() {
      if (this.product.stock_quantity <= 0) return
      this.$emit('exchange', this.product)
    }
  }
}
</script>

<style lang="scss" scoped>
.product-card {
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  
  &.horizontal {
    display: flex;
    
    .product-image {
      width: 160rpx;
      height: 160rpx;
      flex-shrink: 0;
    }
    
    .product-info {
      flex: 1;
      padding: 20rpx;
    }
  }
}

.product-image {
  position: relative;
  width: 100%;
  height: 300rpx;
}

.image {
  width: 100%;
  height: 100%;
}

.tags {
  position: absolute;
  top: 12rpx;
  right: 12rpx;
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.tag {
  padding: 6rpx 12rpx;
  font-size: 20rpx;
  color: #fff;
  border-radius: 12rpx;
}

.hot-tag {
  background-color: #ff4757;
}

.new-tag {
  background-color: #2ed573;
}

.out-of-stock-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.out-of-stock-text {
  color: #fff;
  font-size: 32rpx;
  font-weight: bold;
}

.product-info {
  padding: 20rpx;
}

.product-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-desc {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 12rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-price {
  margin-bottom: 12rpx;
}

.points-price {
  display: flex;
  align-items: baseline;
  margin-bottom: 4rpx;
}

.price-number {
  font-size: 32rpx;
  font-weight: bold;
  color: #ff4757;
}

.price-unit {
  font-size: 22rpx;
  color: #ff4757;
  margin-left: 4rpx;
}

.original-price {
  font-size: 22rpx;
  color: #999;
  text-decoration: line-through;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-stock {
  font-size: 24rpx;
}

.in-stock {
  color: #2ed573;
}

.out-of-stock {
  color: #ff4757;
}

.product-actions {
  flex-shrink: 0;
}

.action-btn {
  padding: 8rpx 20rpx;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 20rpx;
  font-size: 24rpx;
  
  &:disabled {
    background-color: #ccc;
    color: #999;
  }
}

// 水平布局样式调整
.product-card.horizontal {
  .product-image {
    border-radius: 8rpx 0 0 8rpx;
  }
  
  .product-name {
    font-size: 30rpx;
  }
  
  .price-number {
    font-size: 36rpx;
  }
}
</style>